home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 002 / wc110.arc / WC.C next >
Encoding:
C/C++ Source or Header  |  1985-12-10  |  5.5 KB  |  243 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #define    CII        /* Define for Computer Innovations C compiler */
  5.  
  6. /***
  7.  *    wc.c 1.10 [12/10/85] - Count pages, lines, words, and
  8.  *                           characters in file.
  9.  *
  10.  *    Usage:  wc [-p[#]lwc] [names]
  11.  *
  12.  *        Flags:
  13.  *            p - count pages.  Trailing number indicates number
  14.  *                of lines per page.
  15.  *            l - count lines.
  16.  *            w - count words.
  17.  *            c - count characters.
  18.  *
  19.  *        Flags l, w, and c are default.  Pages are defined as
  20.  *        either # lines (defualt 66) or a form-feed character.
  21.  *        Words are delimited by any non-alphabetic character.
  22.  */
  23.      
  24. /*
  25.  *    Copyright, 1984, David L. Messer - All rights reserved.
  26.  *
  27.  *    Permission is granted for the copying and use of this program
  28.  *    if the following conditions are met:
  29.  *
  30.  *        1)    The use is not for direct commercial benefit.
  31.  *            Permission is NOT granted to sell this program
  32.  *            or to incorporate it in any product or for use
  33.  *            as a promotional tool or inducement to buy any
  34.  *            product or service.
  35.  *        2)    The user agrees that this program is distributed
  36.  *            with no warrenties, expressed or implied, as to
  37.  *            its suitablity for use for any purpose.  And the
  38.  *            user assumes full responsibility for any damages
  39.  *            resulting in the use or the inability to use this
  40.  *            program.
  41.  *        3)    That this entire comment block is included in any
  42.  *            copies or modifications of this program.
  43.  *
  44.  *    If you find this program to be of use to you, a donation of
  45.  *    whatever you think it is worth will be cheerfully accepted.
  46.  *
  47.  *    Written by: David L. Messer
  48.  *                P.O. Box 19130, Mpls, MN,  55119
  49.  */
  50.  
  51. char *usage[] = {
  52.     "wc 1.10 [12/10/85] - Count pages, lines, words, and characters in file.\n",
  53.     "Copyright, 1984, David L. Messer - All rights reserved.\n",
  54.     "\n",
  55.     "Usage:  wc [-p[#]lwc] [names]\n",
  56.     "\n",
  57.     "    Flags:\n",
  58.     "        p - count pages.  Trailing number indicates number\n",
  59.     "            of lines per page.\n",
  60.     "        l - count lines.\n",
  61.     "        w - count words.\n",
  62.     "        c - count characters.\n",
  63.     "\n",
  64.     "    Flags l, w, and c are default.  Pages are defined as\n",
  65.     "    either # lines (default 66) or a form-feed character.\n",
  66.     "    Words are delimited by any non-alphabetic character.\n",
  67.     "\n",
  68.     "\n",
  69.     "If this program is of value to you, donations will be cheerfully\n",
  70.     "accepted.  Send to:\n",
  71.     "\n",
  72.     "    David L. Messer\n",
  73.     "    P.O. Box 19130\n",
  74.     "    Mpls, MN  55119\n",
  75.     NULL } ;
  76.  
  77.  
  78. int pagelen = 66 ;    /* Number of lines per page */
  79.  
  80. int pflag ;        /* Count pages */
  81. int lflag ;        /* Count lines */
  82. int wflag ;        /* Count words */
  83. int cflag ;        /* Count characters */
  84.  
  85. long pcount ;    /* Count of pages */
  86. long lcount ;    /* Count of lines */
  87. long wcount ;    /* Count of words */
  88. long ccount ;    /* Count of characters */
  89.  
  90. long ptotal ;    /* Total count of pages */
  91. long ltotal ;    /* Total count of lines */
  92. long wtotal ;    /* Total count of words */
  93. long ctotal ;    /* Total count of characters */
  94.  
  95.  
  96.  
  97. main( argc, argv )
  98. int argc ;
  99. char **argv ;
  100. {
  101.     FILE *f ;
  102.     char *cp ;
  103.     int tflag ;
  104.     int i ;
  105.  
  106.     /*
  107.      * Get flags.
  108.      */
  109.  
  110.     while( --argc > 0 && **++argv == '-' ) {
  111.         cp = *argv ;
  112.         while( *++cp ) {
  113.             if( *cp == 'p' ) {
  114.                 pflag++ ;
  115.                 if( isdigit( *(cp+1) ) ) {
  116.                     pagelen = 0 ;
  117.                     do {
  118.                         cp++ ;
  119.                         pagelen *= 10 ;
  120.                         pagelen += (*cp - '0') ;
  121.                         } while( isdigit( *(cp+1) ) ) ;
  122.                     }
  123.                 }
  124.             else if( *cp == 'l' ) lflag++ ;
  125.             else if( *cp == 'w' ) wflag++ ;
  126.             else if( *cp == 'c' ) cflag++ ;
  127.             else {
  128.                 for( i = 0; usage[i] != NULL; i++ ) {
  129.                     fprintf( stderr, "%s", usage[i] ) ;
  130.                     }
  131.                 exit( 1 ) ;
  132.                 }
  133.             }
  134.         }
  135.  
  136.     if( !pflag && !lflag && !wflag && !cflag ) {
  137.         lflag =
  138.             wflag =
  139.             cflag = 1 ;
  140.         }
  141.  
  142.     /*
  143.      * Process files.
  144.      */
  145.  
  146.     tflag = argc > 1 ;
  147.  
  148.     if( argc > 0 ) {
  149.         while( argc ) {
  150.             argc-- ;
  151. #ifdef CII
  152.             f = fopen( *argv, "rb" ) ;
  153. #else
  154.             f = fopen( *argv, "r" ) ;
  155. #endif
  156.             if( f == NULL ) {
  157.                 fprintf( stderr, "wc:  cannot open %s\n", *argv ) ;
  158.                 }
  159.             else {
  160.                 count( f ) ;
  161.                 fclose( f ) ;
  162.                 if( pflag ) printf( "%7ld", pcount ) ;
  163.                 if( lflag ) printf( " %6ld", lcount ) ;
  164.                 if( wflag ) printf( " %6ld", wcount ) ;
  165.                 if( cflag ) printf( " %6ld", ccount ) ;
  166.                 printf( " %s\n", *argv ) ;
  167.                 }
  168.             argv++ ;
  169.             }
  170.         if( tflag ) {
  171.             if( pflag ) printf( "%7ld", ptotal ) ;
  172.             if( lflag ) printf( " %6ld", ltotal ) ;
  173.             if( wflag ) printf( " %6ld", wtotal ) ;
  174.             if( cflag ) printf( " %6ld", ctotal ) ;
  175.             printf( " total\n" ) ;
  176.             }
  177.         }
  178.     else {
  179.         count( stdin ) ;
  180.         if( pflag ) printf( "%7ld", pcount ) ;
  181.         if( lflag ) printf( " %6ld", lcount ) ;
  182.         if( wflag ) printf( " %6ld", wcount ) ;
  183.         if( cflag ) printf( " %6ld", ccount ) ;
  184.         printf( " \n" ) ;
  185.         }
  186.  
  187.     exit( 0 ) ;
  188.  
  189.     } /* main */
  190.  
  191.  
  192.  
  193.  
  194. /**
  195.  *    count - count lines, words, and characters of a file.
  196.  */
  197.  
  198. count( f )
  199. FILE *f ;
  200. {
  201.     register int c ;
  202.     register int word = 0 ;
  203.     register int plines ;
  204.  
  205.     pcount =
  206.         lcount =
  207.         wcount =
  208.         ccount = 0L ;
  209.     plines = 32767 ;
  210.  
  211.     while( ( c = fgetc( f ) ) >= 0 ) {
  212.         if( plines >= pagelen &&
  213.                 (isprint(c) || c == '\n' || c == '\f') ) {
  214.             pcount++ ;
  215.             plines = 0 ;
  216.             }
  217.  
  218.         ccount++ ;
  219.  
  220. #ifdef CII
  221.         if( !isspace( c ) && c != '\r' && c != '\v' ) word = 1 ;
  222. #else
  223.         if( !isspace( c ) ) word = 1 ;
  224. #endif
  225.         else {
  226.             if( word ) wcount++ ;
  227.             word = 0 ;
  228.             }
  229.  
  230.         if( c == '\n' || c == '\f' ) {
  231.             lcount++ ;
  232.             plines++ ;
  233.             }
  234.         if( c == '\f' ) plines = 32767 ;
  235.         }
  236.  
  237.     ptotal += pcount ;
  238.     ltotal += lcount ;
  239.     wtotal += wcount ;
  240.     ctotal += ccount ;
  241.  
  242.     } /* count */
  243.